package com.android.love.net;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.util.SparseArray;

import com.android.love.callback.DataCallback;
import com.android.love.db.DatabaseUtil;
import com.android.love.parser.AddressParser;
import com.android.love.parser.BaseParser;
import com.android.love.parser.CatalogParser;
import com.android.love.parser.CommentParser;
import com.android.love.parser.DeleteAddressParser;
import com.android.love.parser.MeixiuOrderParser;
import com.android.love.parser.MeixiuParser;
import com.android.love.parser.MyAddressParser;
import com.android.love.parser.MyOrderParser;
import com.android.love.parser.OrderModifyParser;
import com.android.love.parser.OrderParser;
import com.android.love.parser.PayNumParser;
import com.android.love.parser.PhotoParser;
import com.android.love.parser.PushParser;
import com.android.love.parser.RegisterParser;
import com.android.love.parser.ShopDetailParser;
import com.android.love.ui.BasePageActivity.BaseHandler;
import com.android.love.util.Constant;
import com.android.love.util.LogUtil;
import com.android.love.vo.Address;
import com.android.love.vo.Goods;
import com.android.love.vo.Meixiu;
import com.android.love.vo.MeixiuOrder;
import com.android.love.vo.MyRequest;
import com.android.love.vo.Order;
import com.android.love.vo.OrderId;
import com.android.love.vo.UserInfo;

/**
 * 异步任务类
 * 
 * @author adison
 * 
 */
public class NetWorkTask extends BetterAsyncTask<Object, Integer, Object>
		implements Constant {
	private static final String TAG = "NetWorkTask";
	// private Context context;
	private int tag;
	private DataCallback callback;
	// private MyHandler handler;
	private static NetWorkTask workTask;
	private BaseParser parser;
	private MyRequest req;
	/** 是否展示提醒 **/
	private boolean show;
	/** 内容列表 **/
	private boolean get;
	private Handler handler;

	//
	public NetWorkTask(Context context, boolean show, boolean get,
			BaseHandler handler) {
		super(context);
		workTask = this;
		// this.handler = handler;
		this.show = show;
		this.get = get;
		this.handler = handler;
	}

	@Override
	protected void before(Context context) {
		LogUtil.i(TAG, "before--");
		if (show) {
			handler.sendEmptyMessage(LOADING);
		}
		super.before(context);
	}

	/**
	 * 后台线程任务
	 */
	@Override
	protected Object doCheckedInBackground(Context context, Object... params)
			throws Exception {
		LogUtil.i(TAG, "doCheckedInBackground--");
		if (params[0] instanceof DataCallback) {
			callback = (DataCallback) params[0];
		}
		if (params[0] instanceof Context) {
			context = (Context) params[0];
		}
		DataHandler mode = new DataHandler(context);
		tag = (Integer) params[1];
		req = (MyRequest) params[2];
		switch (tag) {
		case LOAD_HOME:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			SparseArray<List> comments = (SparseArray<List>) params[3];
			parser = new CommentParser();
			return mode.getData(req.authority + req.path, req.paramers,
					comments, parser, get);
		case MEIXIU:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			List<Meixiu> meixius = (List<Meixiu>) params[3];
			parser = new MeixiuParser();
			return mode.getData(req.authority + req.path, req.paramers,
					meixius, parser, get);
		case ADDRESS_LIST:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			List<Address> addressList = (List<Address>) params[3];
			parser = new MyAddressParser();
			return mode.getData(req.authority + req.path, req.paramers,
					addressList, parser, get);
		case ORDER_LIST:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			List<Order> orders = (List<Order>) params[3];
			parser = new MyOrderParser();
			return mode.getData(req.authority + req.path, req.paramers, orders,
					parser, get);
		case CATALOG:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			List<Goods> goods = (List<Goods>) params[3];
			parser = new CatalogParser();
			return mode.getData(req.authority + req.path, req.paramers, goods,
					parser, get);
		case GOODS:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			Goods good = (Goods) params[3];
			parser = new ShopDetailParser();
			return mode.getData(req.authority + req.path, req.paramers, good,
					parser, get);
		case PHOTO:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			String[] imgs = (String[]) params[3];
			parser = new PhotoParser();
			return mode.getData(req.authority + req.path, req.paramers, imgs,
					parser, get);
		case GET_USER_INFO:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			UserInfo info = (UserInfo) params[3];
			parser = new RegisterParser();
			return mode.getData(req.authority + req.path, req.paramers, info,
					parser, get);
		case ADDRESS_ADD:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			long address_id = (Long) params[3];
			parser = new AddressParser();
			return mode.getData(req.authority + req.path, req.paramers,
					address_id, parser, get);
		case PUSH:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			int type = (Integer) params[3];
			parser = new PushParser();
			return mode.getData(req.authority + req.path, req.paramers, type,
					parser, get);
		case ORDER_MODIFY:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			int status = (Integer) params[3];
			parser = new OrderModifyParser();
			return mode.getData(req.authority + req.path, req.paramers, status,
					parser, get);
		case TIJIAO:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
//			long order_id = (Long) params[3];
			String[] orderid=(String[]) params[3];
			parser = new OrderParser();
			return mode.getData(req.authority + req.path, req.paramers,
					orderid, parser, get);
		case MEIUXIUTIJIAO:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
//			long order_id = (Long) params[3];
			MeixiuOrder meixiuOrder=(MeixiuOrder) params[3];
			parser = new MeixiuOrderParser();
			return mode.getData(req.authority + req.path, req.paramers,
					meixiuOrder, parser, get);
		case ADDRESS_DELETE:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			boolean delete_result = (Boolean) params[3];
			parser = new DeleteAddressParser();
			return mode.getData(req.authority + req.path, req.paramers,
					delete_result, parser, get);
		case ADDRESS_CHECK:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			boolean result = (Boolean) params[3];
			parser = new DeleteAddressParser();
			return mode.getData(req.authority + req.path, req.paramers, result,
					parser, get);
		case CART:
			return DatabaseUtil.getInstance(context).queryCart();
		case NOT_PAY_NUM:
			LogUtil.i(TAG, "req--" + req.authority + req.path);
			Integer pay_num = (Integer) params[3];
			parser = new PayNumParser();
			return mode.getData(req.authority + req.path, req.paramers,
					pay_num, parser, get);
		}
		return super.doCheckedInBackground(context, params);
	}

	/**
	 * 取消线程
	 */
	public static void cancelTask() {
		if (workTask != null) {
			if (!workTask.isCancelled()) {
				workTask.cancel(true);
			}
		}
	}

	@Override
	protected void handleError(Context context, Exception error) {
		LogUtil.i(TAG, "error--");
		if (error != null) {
			LogUtil.i(TAG, "error--" + error.getMessage());
		}
	}

	@Override
	protected void after(Context context, Object result) {
		LogUtil.i(TAG, "after--");
		if (context instanceof DataCallback) {
			if (callback != null) {
				LogUtil.i(TAG, "after--111");
				callback.handle(tag, result);
			}
		}

	}

}
